﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace FrontDisplay
{
    /// <summary>
    /// Interaction logic for ParametersWindow.xaml
    /// </summary>
    public partial class ParametersWindow : Window
    {
        private PluginParametersCollection _pluginsParamenterControls;

        public ParametersWindow()
        {
            InitializeComponent();

            Icon = Application.Current.MainWindow.Icon;

            Topmost = Application.Current.MainWindow.Topmost;

            AddHandler(Selector.SelectionChangedEvent, new SelectionChangedEventHandler(EditsChanged));
            AddHandler(TextBoxBase.TextChangedEvent, new TextChangedEventHandler(EditsChanged));
            AddHandler(ToggleButton.CheckedEvent, new RoutedEventHandler(EditsChanged));
            AddHandler(ToggleButton.UncheckedEvent, new RoutedEventHandler(EditsChanged));

            //TODO: move to XAML
            foreach (ColorTheme item in ThemeManager.ColorThemes)
            {
                ComboBoxItem comboBoxItem = new ComboBoxItem {Content = item};
                if (item.Name == Global.Config.Theme)
                {
                    comboBoxItem.IsSelected = true;
                }
                ThemeComboBox.Items.Add(comboBoxItem);
            }

            //TODO: move to XAML
            foreach (IBackground item in ThemeManager.Backgrounds)
            {
                ComboBoxItem comboBoxItem = new ComboBoxItem { Content = item.Name };
                if (item.Name == Global.Config.Background)
                {
                    comboBoxItem.IsSelected = true;
                }
                BackgroundComboBox.Items.Add(comboBoxItem);
            }

            //TODO: move to XAML
            int monitorNumber = 0;
            foreach (var item in System.Windows.Forms.Screen.AllScreens)
            {
                ComboBoxItem comboBoxItem =
                    new ComboBoxItem
                        {
                            Content =
                                string.Format("{0}: {1}x{2}", monitorNumber+1, item.Bounds.Width, item.Bounds.Height)
                        };

                if (monitorNumber == Global.Config.DefaultMonitor)
                {
                    comboBoxItem.IsSelected = true;
                }
                MonitorComboBox.Items.Add(comboBoxItem);
                monitorNumber++;
            }

            //TODO: move to XAML
            foreach (CultureInfo item in Def.Localization.AvailableLanguages)
            {
                ComboBoxItem comboBoxItem =
                    new ComboBoxItem
                    {
                        Content = item
                    };

                if (item.TwoLetterISOLanguageName == Global.Config.Language)
                {
                    comboBoxItem.IsSelected = true;
                }
                LanguageComboBox.Items.Add(comboBoxItem);
            }

            InterceptMouseCheckBox.IsChecked = Global.Config.InterceptMouse;
            IsAutoChangeThemeCheckBox.IsChecked = Global.Config.IsAutoChangeTheme;
            ChangeThemeIntervalTextBox.Text = Global.Config.ChangeThemeInterval.ToString();

            //InfoHotkeyCheckBox.IsChecked = !Global.Config.InfoHotkey.IsNull;
            //InfoHotkey.Value = Global.Config.InfoHotkey;

            Loaded += ParametersWindowLoaded;

            _pluginsParamenterControls = new PluginParametersCollection(MainWindow.Plugins, TabControl);
        }

        private void ParametersWindowLoaded(object sender, RoutedEventArgs e)
        {
            ApplyButton.IsEnabled = false;
        }

        private void EditsChanged(object sender, RoutedEventArgs e)
        {
            int changeThemeInterval;
            if (Int32.TryParse(ChangeThemeIntervalTextBox.Text, out changeThemeInterval))
            {
                if (changeThemeInterval < 1) { changeThemeInterval = 1; }
                ChangeThemeIntervalTextBox.Text = changeThemeInterval.ToString();
            }
            else
            {
                ChangeThemeIntervalTextBox.Text = "1";
            }
            ApplyButton.IsEnabled = true;
        }

        private void ApplyParameters()
        {
            if (BackgroundComboBox.SelectedItem != null)
            {
                Global.Config.Background = ((ComboBoxItem)BackgroundComboBox.SelectedItem).Content.ToString();
            }

            if (Global.Config.DefaultMonitor != MonitorComboBox.SelectedIndex)
            {
                Global.Config.DefaultMonitor = MonitorComboBox.SelectedIndex;
                ((MainWindow) (Application.Current.MainWindow)).UpdateWindowPosition();
            }

            Global.Config.InterceptMouse = InterceptMouseCheckBox.IsChecked.HasValue ? InterceptMouseCheckBox.IsChecked.Value : false;
            Global.Config.ChangeThemeInterval = Convert.ToInt32(ChangeThemeIntervalTextBox.Text);
            Global.Config.IsAutoChangeTheme = IsAutoChangeThemeCheckBox.IsChecked.HasValue ? IsAutoChangeThemeCheckBox.IsChecked.Value : false;

            Global.Config.Language = ((CultureInfo)((ComboBoxItem)LanguageComboBox.SelectedItem).Content).TwoLetterISOLanguageName;

            //Global.Config.InfoHotkey = InfoHotkey.Value;

            //TODO: change color updating
            if (themeExpander.IsExpanded)
            {
                string themeName = themeNameTextbox.Text.Trim();
                ColorTheme colorTheme = ThemeManager.GetThemeByName(themeName) ?? new ColorTheme();

                colorTheme.Name = themeName;
                colorTheme.TextForeground = textColor.SelectedColor;
                colorTheme.TextShadow = shadowColor.SelectedColor;
                colorTheme.BackgroundTop = backTopColor.SelectedColor;
                colorTheme.Background = backColor.SelectedColor;
                colorTheme.BackgroundBottom = backBottomColor.SelectedColor;
                colorTheme.SaveToFile();

                Global.Config.Theme = colorTheme.Name;
            }
            else
            {
                if (ThemeComboBox.SelectedItem != null)
                {
                    Global.Config.Theme = ((ComboBoxItem) ThemeComboBox.SelectedItem).Content.ToString();
                }
            }

            _pluginsParamenterControls.SaveConfig();

            Global.Config.Save();
        }

        private void OkClick(object sender, RoutedEventArgs e)
        {
            ApplyParameters();
            Close();
        }

        private void CancelClick(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void ApplyClick(object sender, RoutedEventArgs e)
        {
            ApplyParameters();
            ApplyButton.IsEnabled = false;
        }

        private void ThemeComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ThemeComboBox.SelectedItem == null) return;

            ColorTheme theme = ((ColorTheme)((ComboBoxItem)ThemeComboBox.SelectedItem).Content);

            themeNameTextbox.Text = theme.Name;
            textColor.SelectedColor = theme.TextForeground;
            shadowColor.SelectedColor = theme.TextShadow;
            backTopColor.SelectedColor = theme.BackgroundTop;
            backColor.SelectedColor = theme.Background;
            backBottomColor.SelectedColor = theme.BackgroundBottom;
        }
    }
}
